package com.cyms.modules.base.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.Query;

import com.cyms.modules.base.dao.LineDao;
import com.cyms.modules.base.entity.LineEntity;
import com.cyms.modules.base.entity.LineScheduleEntity;
import com.cyms.modules.base.entity.LineTypeEntity;
import com.cyms.modules.base.service.LineScheduleService;
import com.cyms.modules.base.service.LineService;
import com.cyms.modules.base.service.LineTypeService;


@Service("lineService")
public class LineServiceImpl extends ServiceImpl<LineDao, LineEntity> implements LineService {

	@Autowired
	private LineScheduleService lineScheduleService;
	@Autowired
	private LineTypeService lineTypeService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	String code=(String) params.get("code");
    	String name=(String) params.get("name");
    	String typeId=(String) params.get("typeId");
		Long createOrg = (Long) params.get("createOrg");
    	//
    	List<LineTypeEntity> lineTypeList=lineTypeService.selectList(new EntityWrapper<LineTypeEntity>().like("pids", ","+typeId+","));
    	//
        Page<LineEntity> page = this.selectPage(
                new Query<LineEntity>(params).getPage(),
                new EntityWrapper<LineEntity>()
						.eq(createOrg != null,"create_org",createOrg)
						.orderBy("create_date", false)
						.like(StringUtils.isNotBlank(code),"code",code)
						.like(StringUtils.isNotBlank(name),"name",name)
						.in(lineTypeList!=null&&lineTypeList.size()>0,"type_id", lineTypeList.parallelStream().map((lineType)->lineType.getId()).collect(Collectors.toList()))
        );
        for (LineEntity line : page.getRecords()) {
        	List<LineScheduleEntity> list=lineScheduleService.selectList(new EntityWrapper<LineScheduleEntity>().eq("line_id", line.getId()));
			line.setScheduleList(list);
		}
        return new PageUtils(page);
    }

}
